package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.PageParam;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetMealService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/setmeal")
@RestController
@Api(tags="套餐管理")
public class SetMealController {
    
    @Autowired
    private SetMealService setMealService;

    //修改套餐状态
    @PostMapping("/status")
    public R updateStatus(@RequestBody Setmeal setmeal) {
        //调用service
        int rows = setMealService.updateStatus(setmeal);

        if (rows > 0) {
            return R.success("修改状态成功！");
        } else {
            return R.error("修改状态失败！");
        }
    }
    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto) {
        //调用service
        int rows = setMealService.update(setmealDto);

        if (rows > 0) {
            return R.success("更新套餐成功！");
        } else {
            return R.error("更新套餐失败！");
        }
    }
    /**
     * 删除套餐（包含单个删除和批量删除）
     * @param ids
     * @return
     */
    //后台删除一个套餐数据 缓存套餐数据全部删除
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        
        int row = setMealService.delete(ids);
        if(row >0){
            return R.success("删除成功~！");
        }else{
            return R.success("删除失败~！");
        }
    
    }
    
    /**
     * 分页查询套餐
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParam pageParam){
        
        //1. 调用service
        IPage<SetmealDto> page = setMealService.findPage(pageParam);
        
        //2. 返回
        return R.success(page);
    
    
    }
    
    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @CacheEvict(key = "#setmealDto.categoryId+'_'+#setmealDto.status", cacheNames = "setmealCache")
    @PostMapping
    public R add(@RequestBody SetmealDto setmealDto){
        
        //1. 调用service
        int row = setMealService.add(setmealDto);
        
        //2. 判断
        if(row > 0 ){
            return R.success("新增套餐成功！");
        }else{
            return R.error("新增套餐失败！");
        }
        
    }

    /**
     * 根据分类id和状态查询套餐数据
     * @param categoryId
     * @param status
     * @return
     */
    @Cacheable(key = "#categoryId+'_'+#status", cacheNames = "setmealCache")
    @GetMapping("/list")
    public R findCategoryId(Long categoryId, Integer status) {

        //调用service
        List<Setmeal> list = setMealService.findCategoryId(categoryId, status);

        return R.success(list);
    }
}
